// electron/handler/TerminalHandlers.ts
import { ipcMain } from 'electron';
import TerminalController from '../controller/TerminalController';

export function setupTerminalHandlers() {
  ipcMain.on('terminal:start', (event) => {
    TerminalController.setupTerminalEvents(event);
  });

  ipcMain.on('terminal:write', (event, data: string) => {
    TerminalController.write(data, event);
  });

  ipcMain.on('terminal:resize', (event, { cols, rows }) => {
    TerminalController.resize(cols, rows, event);
  });

  ipcMain.on('terminal:stop', (event) => {
    TerminalController.stop(event);
  });

  ipcMain.on('terminal:exec-python-file', (event, filePath: string) => {
    TerminalController.execPythonFile(filePath, event);
  });

  // 返回清理函数，方便应用退出时调用
  return () => {
    ipcMain.removeAllListeners('terminal:start');
    ipcMain.removeAllListeners('terminal:write');
    ipcMain.removeAllListeners('terminal:resize');
  };
}
